package day12;

public class 链表的第一个公共节点 {
	public ListNode getIntersectionNode(ListNode h1, ListNode h2) {
		if(h1==null||h2==null) {
			return null;
		}
		ListNode a=h1,b=h2;
		int diff=0;
		while(a!=null) {
			a=a.next;
			diff++;
		}
		while(b!=null) {
			b=b.next;
			diff--;
		}
		if(a!=b) return null;
		if(diff>=0) {
			a=h1;
			b=h2;
		}
		else {
			a=h2;
			b=h1;
		}
		diff=Math.abs(diff);
		while(diff-->0) {
			a=a.next;
		}
		while(a!=b) {
			a=a.next;
			b=b.next;
		}
		return a;
	}
}
